// Mock数据
export const mockData = {
  // 用户数据
  users: [
    {
      id: 1,
      username: 'patient1',
      password: '123456',
      name: '张三',
      phone: '13800138001',
      idCard: '110101199001011234',
      gender: 'male',
      birthday: '1990-01-01',
      address: '北京市朝阳区某某街道某某小区',
      avatar: '',
      role: 'patient',
      permissions: ['view_appointments', 'create_appointments', 'cancel_appointments']
    },
    {
      id: 2,
      username: 'patient2',
      password: '123456',
      name: '李四',
      phone: '13800138002',
      idCard: '110101199002022345',
      gender: 'female',
      birthday: '1990-02-02',
      address: '北京市海淀区某某街道某某小区',
      avatar: '',
      role: 'patient',
      permissions: ['view_appointments', 'create_appointments', 'cancel_appointments']
    },
    {
      id: 3,
      username: 'doctor1',
      password: '123456',
      name: '王医生',
      phone: '13800138003',
      idCard: '110101198001011234',
      gender: 'male',
      birthday: '1980-01-01',
      address: '北京市西城区某某街道某某小区',
      avatar: '',
      role: 'doctor',
      department: 1,
      title: '主任医师',
      permissions: ['view_appointments', 'manage_appointments', 'view_patients', 'manage_schedule']
    },
    {
      id: 4,
      username: 'admin',
      password: '123456',
      name: '系统管理员',
      phone: '13800138004',
      idCard: '110101197001011234',
      gender: 'male',
      birthday: '1970-01-01',
      address: '北京市东城区某某街道某某小区',
      avatar: '',
      role: 'admin',
      permissions: ['view_all', 'manage_users', 'manage_doctors', 'manage_departments', 'view_statistics', 'manage_system']
    }
  ],

  // 科室数据
  departments: [
    {
      id: 1,
      name: '内科',
      description: '内科是临床医学的一个分支，主要治疗内脏器官疾病',
      icon: '🏥',
      doctors: [1, 2, 3]
    },
    {
      id: 2,
      name: '外科',
      description: '外科是临床医学的一个分支，主要进行手术治疗',
      icon: '⚕️',
      doctors: [4, 5, 6]
    },
    {
      id: 3,
      name: '儿科',
      description: '儿科专门治疗儿童疾病，包括新生儿到18岁青少年',
      icon: '👶',
      doctors: [7, 8]
    },
    {
      id: 4,
      name: '妇产科',
      description: '妇产科专门治疗女性生殖系统疾病和妊娠相关疾病',
      icon: '👩‍⚕️',
      doctors: [9, 10]
    },
    {
      id: 5,
      name: '眼科',
      description: '眼科专门治疗眼部疾病和视力问题',
      icon: '👁️',
      doctors: [11, 12]
    },
    {
      id: 6,
      name: '口腔科',
      description: '口腔科专门治疗口腔、牙齿和颌面部疾病',
      icon: '🦷',
      doctors: [13, 14]
    }
  ],

  // 医生数据
  doctors: [
    {
      id: 1,
      name: '王医生',
      title: '主任医师',
      department: 1,
      specialty: '心血管疾病、高血压、冠心病',
      experience: '20年',
      avatar: new URL('../assets/imgs/头像1.webp', import.meta.url).href,
      schedule: [
        { day: '周一', time: '上午', available: true },
        { day: '周二', time: '下午', available: true },
        { day: '周三', time: '上午', available: false },
        { day: '周四', time: '下午', available: true },
        { day: '周五', time: '上午', available: true }
      ]
    },
    {
      id: 2,
      name: '李医生',
      title: '副主任医师',
      department: 1,
      specialty: '消化系统疾病、胃炎、胃溃疡',
      experience: '15年',
      avatar: new URL('../assets/imgs/头像2.webp', import.meta.url).href,
      schedule: [
        { day: '周一', time: '下午', available: true },
        { day: '周二', time: '上午', available: true },
        { day: '周三', time: '下午', available: true },
        { day: '周四', time: '上午', available: false },
        { day: '周五', time: '下午', available: true }
      ]
    },
    {
      id: 3,
      name: '张医生',
      title: '主治医师',
      department: 1,
      specialty: '呼吸系统疾病、哮喘、肺炎',
      experience: '10年',
      avatar: new URL('../assets/imgs/头像3.webp', import.meta.url).href,
      schedule: [
        { day: '周一', time: '上午', available: true },
        { day: '周二', time: '下午', available: false },
        { day: '周三', time: '上午', available: true },
        { day: '周四', time: '下午', available: true },
        { day: '周五', time: '上午', available: true }
      ]
    },
    {
      id: 4,
      name: '陈医生',
      title: '主任医师',
      department: 2,
      specialty: '普外科手术、腹腔镜手术',
      experience: '25年',
      avatar: new URL('../assets/imgs/头像4.webp', import.meta.url).href,
      schedule: [
        { day: '周一', time: '上午', available: true },
        { day: '周二', time: '下午', available: true },
        { day: '周三', time: '上午', available: true },
        { day: '周四', time: '下午', available: true },
        { day: '周五', time: '上午', available: false }
      ]
    },
    {
      id: 5,
      name: '刘医生',
      title: '副主任医师',
      department: 2,
      specialty: '骨科手术、关节置换',
      experience: '18年',
      avatar: new URL('../assets/imgs/头像5.webp', import.meta.url).href,
      schedule: [
        { day: '周一', time: '下午', available: true },
        { day: '周二', time: '上午', available: true },
        { day: '周三', time: '下午', available: true },
        { day: '周四', time: '上午', available: true },
        { day: '周五', time: '下午', available: true }
      ]
    },
    {
      id: 6,
      name: '赵医生',
      title: '主治医师',
      department: 2,
      specialty: '微创手术、疝气手术',
      experience: '12年',
      avatar: new URL('../assets/imgs/头像6.webp', import.meta.url).href,
      schedule: [
        { day: '周一', time: '上午', available: true },
        { day: '周二', time: '下午', available: true },
        { day: '周三', time: '上午', available: true },
        { day: '周四', time: '下午', available: true },
        { day: '周五', time: '上午', available: true }
      ]
    },
    {
      id: 7,
      name: '孙医生',
      title: '主任医师',
      department: 3,
      specialty: '儿童常见病、发热、咳嗽',
      experience: '22年',
      avatar: new URL('../assets/imgs/头像7.jpg', import.meta.url).href,
      schedule: [
        { day: '周一', time: '上午', available: true },
        { day: '周二', time: '下午', available: true },
        { day: '周三', time: '上午', available: true },
        { day: '周四', time: '下午', available: true },
        { day: '周五', time: '上午', available: true }
      ]
    },
    {
      id: 8,
      name: '周医生',
      title: '副主任医师',
      department: 3,
      specialty: '儿童生长发育、营养咨询',
      experience: '16年',
      avatar: new URL('../assets/imgs/头像8.jpg', import.meta.url).href,
      schedule: [
        { day: '周一', time: '下午', available: true },
        { day: '周二', time: '上午', available: true },
        { day: '周三', time: '下午', available: true },
        { day: '周四', time: '上午', available: true },
        { day: '周五', time: '下午', available: true }
      ]
    }
  ],

  // 轮播图数据
  carousel: [
    {
      id: 1,
      title: '医院公告',
      content: '我院新增心血管专科门诊，每周一、三、五上午开诊',
      image: '',
      type: 'notice',
      bgColor: '#4CAF50'
    },
    {
      id: 2,
      title: '健康科普',
      content: '春季养生小贴士：注意保暖，多喝水，适当运动',
      image: '',
      type: 'health',
      bgColor: '#2196F3'
    },
    {
      id: 3,
      title: '医生提醒',
      content: '王医生提醒：高血压患者请定期监测血压，按时服药',
      image: '',
      type: 'reminder',
      bgColor: '#FF9800'
    }
  ],

  // 预约数据 - 初始为空，只有用户实际预约后才有数据
  appointments: [
    {
      id: 1,
      userId: 1,
      doctorId: 3, // 对应doctor1用户
      doctorName: '王医生',
      departmentName: '内科',
      appointmentDate: '2025-07-15',
      appointmentTime: '上午',
      patientName: '张三',
      patientPhone: '13800138001',
      symptoms: '胸闷气短，血压偏高，想咨询心血管方面的问题',
      status: 'pending',
      createTime: '2025-07-10 10:30:00'
    },
    {
      id: 2,
      userId: 2,
      doctorId: 3, // 对应doctor1用户
      doctorName: '王医生',
      departmentName: '内科',
      appointmentDate: '2025-07-16',
      appointmentTime: '下午',
      patientName: '李四',
      patientPhone: '13800138002',
      symptoms: '最近经常胃痛，消化不良，想检查一下消化系统',
      status: 'confirmed',
      createTime: '2025-07-11 14:20:00'
    }
  ],

  // AI问答数据
  aiQuestions: [
    {
      id: 1,
      question: '高血压患者应该注意什么？',
      answer: '高血压患者应该：1. 低盐饮食，每日盐摄入量不超过6克；2. 规律运动，每周至少150分钟中等强度运动；3. 戒烟限酒；4. 保持良好心态；5. 定期监测血压；6. 按时服药。',
      category: '心血管'
    },
    {
      id: 2,
      question: '感冒发烧应该怎么办？',
      answer: '感冒发烧时：1. 多休息，保证充足睡眠；2. 多喝水，保持水分；3. 适当降温，可用温水擦浴；4. 清淡饮食；5. 如体温超过38.5度，可服用退烧药；6. 如症状持续或加重，及时就医。',
      category: '呼吸系统'
    },
    {
      id: 3,
      question: '如何预防糖尿病？',
      answer: '预防糖尿病：1. 控制体重，保持健康BMI；2. 规律运动，每周至少150分钟；3. 健康饮食，减少精制糖和饱和脂肪；4. 戒烟限酒；5. 定期体检，监测血糖；6. 保持良好生活习惯。',
      category: '内分泌'
    }
  ]
}

// Mock API函数
export const mockAPI = {
  // 登录
  login: (username, password) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('登录尝试:', { username, password })
        console.log('可用用户:', mockData.users.map(u => ({ username: u.username, password: u.password })))
        
        const user = mockData.users.find(u => u.username === username && u.password === password)
        console.log('找到用户:', user)
        
        if (user) {
          resolve({ success: true, data: user })
        } else {
          reject({ success: false, message: '用户名或密码错误' })
        }
      }, 1000)
    })
  },

  // 获取科室列表
  getDepartments: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ success: true, data: mockData.departments })
      }, 500)
    })
  },

  // 获取医生列表
  getDoctors: (departmentId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const doctors = mockData.doctors.filter(d => d.department === parseInt(departmentId))
        resolve({ success: true, data: doctors })
      }, 500)
    })
  },

  // 获取医生详情
  getDoctorDetail: (doctorId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const doctor = mockData.doctors.find(d => d.id === parseInt(doctorId))
        resolve({ success: true, data: doctor })
      }, 500)
    })
  },

  // 获取轮播图
  getCarousel: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ success: true, data: mockData.carousel })
      }, 300)
    })
  },

  // 创建预约
  createAppointment: (appointmentData) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newAppointment = {
          id: mockData.appointments.length + 1,
          ...appointmentData,
          status: 'pending',
          createTime: new Date().toLocaleString()
        }
        mockData.appointments.push(newAppointment)
        resolve({ success: true, data: newAppointment })
      }, 1000)
    })
  },

  // 获取用户预约列表
  getUserAppointments: (userId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const appointments = mockData.appointments.filter(a => a.userId === parseInt(userId))
        resolve({ success: true, data: appointments })
      }, 500)
    })
  },

  // 获取所有预约（医生权限）
  getAllAppointments: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ success: true, data: mockData.appointments })
      }, 500)
    })
  },

  // 取消预约
  cancelAppointment: (appointmentId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const appointment = mockData.appointments.find(a => a.id === parseInt(appointmentId))
        if (appointment) {
          appointment.status = 'cancelled'
        }
        resolve({ success: true, data: appointment })
      }, 500)
    })
  },

  // AI问答
  aiQuestion: (question) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        // 简单的关键词匹配
        let answer = '抱歉，我暂时无法回答这个问题，建议您咨询专业医生。'
        
        if (question.includes('高血压')) {
          answer = mockData.aiQuestions[0].answer
        } else if (question.includes('感冒') || question.includes('发烧')) {
          answer = mockData.aiQuestions[1].answer
        } else if (question.includes('糖尿病')) {
          answer = mockData.aiQuestions[2].answer
        }
        
        resolve({ success: true, data: { question, answer } })
      }, 1000)
    })
  },

  // 权限管理相关API
  // 检查用户权限
  checkPermission: (userId, permission) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const user = mockData.users.find(u => u.id === parseInt(userId))
        if (user && user.permissions) {
          const hasPermission = user.permissions.includes(permission) || user.permissions.includes('view_all')
          resolve({ success: true, data: { hasPermission } })
        } else {
          resolve({ success: false, data: { hasPermission: false } })
        }
      }, 100)
    })
  },

  // 获取用户角色信息
  getUserRole: (userId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const user = mockData.users.find(u => u.id === parseInt(userId))
        if (user) {
          resolve({ success: true, data: { role: user.role, permissions: user.permissions } })
        } else {
          resolve({ success: false, data: null })
        }
      }, 100)
    })
  },

  // 获取所有用户（管理员权限）
  getAllUsers: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const users = mockData.users.map(u => ({
          id: u.id,
          username: u.username,
          name: u.name,
          role: u.role,
          phone: u.phone,
          department: u.department,
          title: u.title
        }))
        resolve({ success: true, data: users })
      }, 500)
    })
  },

  // 获取系统统计信息（管理员权限）
  getSystemStatistics: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const stats = {
          totalUsers: mockData.users.length,
          totalAppointments: mockData.appointments.length,
          totalDoctors: mockData.doctors.length,
          totalDepartments: mockData.departments.length,
          pendingAppointments: mockData.appointments.filter(a => a.status === 'pending').length,
          confirmedAppointments: mockData.appointments.filter(a => a.status === 'confirmed').length
        }
        resolve({ success: true, data: stats })
      }, 500)
    })
  },

  // 获取所有医生（管理员权限）
  getAllDoctors: () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ success: true, data: mockData.doctors })
      }, 500)
    })
  },

  // 创建科室（管理员权限）
  createDepartment: (departmentData) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newDepartment = {
          id: mockData.departments.length + 1,
          ...departmentData
        }
        mockData.departments.push(newDepartment)
        resolve({ success: true, data: newDepartment })
      }, 500)
    })
  },

  // 更新科室（管理员权限）
  updateDepartment: (departmentData) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const index = mockData.departments.findIndex(d => d.id === departmentData.id)
        if (index !== -1) {
          mockData.departments[index] = { ...mockData.departments[index], ...departmentData }
          resolve({ success: true, data: mockData.departments[index] })
        } else {
          resolve({ success: false, message: '科室不存在' })
        }
      }, 500)
    })
  },

  // 删除科室（管理员权限）
  deleteDepartment: (departmentId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const index = mockData.departments.findIndex(d => d.id === departmentId)
        if (index !== -1) {
          mockData.departments.splice(index, 1)
          resolve({ success: true, message: '删除成功' })
        } else {
          resolve({ success: false, message: '科室不存在' })
        }
      }, 500)
    })
  }
} 